package hc.j2me.ui;

import hc.j2me.ScreenClientManager;

public class DragStopDelayTimer extends UIHCTimer{
	private static final int BORDER_WIDTH = 10;
	public static final int IN_AUTO_MOVING_MODE = -2;
	public static final int IN_READY_AUTO_MOVING_MODE = -1;
	
	//某点长时间停留时间及坐标，供自动移边
	private int stopDragX = -1, stopDragY = -1;
	private int usedStopDragX = -2, usedStopDragY = -2;
	private long stopDragStartMS = IN_READY_AUTO_MOVING_MODE;

	public DragStopDelayTimer(String name, int ms, boolean enable) {
		super(name, ms, enable);
	}
	
	
	public void setEnable(boolean enable) {
//		L.V = L.O ? false : LogManager.log(getName() + ":" + enable);
		super.setEnable(enable);
	}

	public void resetTimerCount() {
//		L.V = L.O ? false : LogManager.log(getName() + ":resetTimerCount");
		super.resetTimerCount();
	}


	public void resetMode(){
//		L.V = L.O ? false : LogManager.log("resetMode");
		synchronized (this) {
	//		if(stopDragStartMS < 0){
				//收到更新后的画面，进入可自动移区模式
				//由于可能存在同时发出横向或纵向，两者回来存在时差，
				//在此不用条件“if(stopDragStartMS < 0){”,即采用后到为起始点，覆盖先到的状态。
	//	}
			stopDragStartMS = IN_READY_AUTO_MOVING_MODE;
			
			if(usedStopDragX == stopDragX && usedStopDragY == stopDragY){
				resetTimerCount();
				setEnable(true);
				
				//承接上一次
				stopDragStartMS = 0;
			}
		}
	}
	
	public void releaseDrag(){
		usedStopDragX = -2;
		usedStopDragY = -2;
		setEnable(false);
	}

	public void checkStart(int x, int y){
//		L.V = L.O ? false : LogManager.log("checkStart");
		synchronized (this) {
			stopDragX = x;
			stopDragY = y;
	
			boolean isMoved = false;
			if(y <= BORDER_WIDTH){
				//自动上移
				isMoved = true;
			}
			if(y >= (ScreenClientManager.getClientHeight() - BORDER_WIDTH)){
				//自动下移
				isMoved = true;
			}
			
			
			if(x <= BORDER_WIDTH){
				//自动左移
				isMoved = true;
			}
			if(x >= (ScreenClientManager.getClientWidth() - BORDER_WIDTH)){
				//自动右移
				isMoved = true;
			}
			
			if(isMoved){
				if(stopDragStartMS == IN_READY_AUTO_MOVING_MODE || stopDragStartMS >= 0){
					resetTimerCount();
					setEnable(true);
					stopDragStartMS = System.currentTimeMillis();
					
					return;
				}
			}
			setEnable(false);
		}
	}
	public void doBiz() {
//		L.V = L.O ? false : LogManager.log("doBiz");
		synchronized (this) {
			int x = stopDragX;
			int y = stopDragY;
			
			if(stopDragStartMS >= 0){
//				L.V = L.O ? false : LogManager.log("MS:" + stopDragStartMS);
				
				//原点停留超过2秒
	
				//检查是否移到边区，则进行尝试自动移块
				int autoMoveYPixelxNum = (int)(ScreenClientManager.getClientHeight() * 0.4);
				int autoMoveXPixelxNum = (int)(ScreenClientManager.getClientWidth() * 0.4);
				
//				L.V = L.O ? false : LogManager.log("Biz x:" + x + ", y:" + y);

				boolean isMoved = false;
				if(y <= BORDER_WIDTH){
					//自动上移
					isMoved |= ScreenClientManager.moveUp(autoMoveYPixelxNum);
				}
				if(y >= (ScreenClientManager.getClientHeight() - BORDER_WIDTH)){
					//自动下移
					isMoved |= ScreenClientManager.moveUp(-1 * autoMoveYPixelxNum);
				}
				
				
				if(x <= BORDER_WIDTH){
					//自动左移
					isMoved |= ScreenClientManager.moveRight(-1 * autoMoveXPixelxNum);
				}
				if(x >= (ScreenClientManager.getClientWidth() - BORDER_WIDTH)){
					//自动右移
					isMoved |= ScreenClientManager.moveRight(autoMoveXPixelxNum);
				}
				
				if(isMoved){
//					L.V = L.O ? false : LogManager.log("Moving area at " + getName());
					
					((HCCtrlGameCanvas)ScreenClientManager.getScreenClient()).setAutoDragedRemote();
					
					//由于系统级时延为3秒，所以等待收到数据后，才进行下一次自动移区
					stopDragStartMS = IN_AUTO_MOVING_MODE;
					usedStopDragX = stopDragX;
					usedStopDragY = stopDragY;
				}else{
//					L.V = L.O ? false : LogManager.log("NO moving area at " + getName());
					usedStopDragX = -2;
					usedStopDragY = -2;
				}
				setEnable(false);
			}
		}
	}

}
